
#include "hal_uart.h"
#include "string.h"
#include "stdarg.h"
#include "stdio.h"
#include<stdint.h>
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
DMA_HandleTypeDef DMA_Init_Handle_uart_rx;
DMA_HandleTypeDef DMA_Init_Handle_uart_tx;


void MX_USART1_UART_Init(void)
{

  
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  

}

void MX_USART2_UART_Init(void)
{

  
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
 

}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
   
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	            
 
  }
   if(uartHandle->Instance==USART2)
  {
	 __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
   
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);  
	HAL_NVIC_SetPriority(USART2_IRQn,6,0);
	HAL_NVIC_EnableIRQ(USART2_IRQn);
	  
	__HAL_RCC_DMA1_CLK_ENABLE();
    DMA_Init_Handle_uart_rx.Instance = DMA1_Stream5;                              //DMA类似于横纵坐标
    DMA_Init_Handle_uart_rx.Init.Channel = DMA_CHANNEL_4;                         //DMA类似于横纵坐标详细看手册
    DMA_Init_Handle_uart_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; 			      //外设到内存
	DMA_Init_Handle_uart_rx.Init.PeriphInc = DMA_PINC_DISABLE;     			   	  //不递增地址
    DMA_Init_Handle_uart_rx.Init.MemInc = DMA_MINC_ENABLE;         		      //递增地址
    DMA_Init_Handle_uart_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;   //1字节
    DMA_Init_Handle_uart_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;      //1字节
    DMA_Init_Handle_uart_rx.Init.Mode = DMA_NORMAL;                             //正常转换
    DMA_Init_Handle_uart_rx.Init.Priority = DMA_PRIORITY_HIGH;                    //速度
    DMA_Init_Handle_uart_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;                 
	HAL_DMA_Init(&DMA_Init_Handle_uart_rx);                                       //初始化dma
	//这里直接强硬调用底层接口，如果使用封装函数直接去读DR寄存器会清楚EOC标志位
	 __HAL_LINKDMA(&huart2,hdmarx,DMA_Init_Handle_uart_rx);	                  //链接adc总控结构体
	  
    HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 5, 0);                        //优先级
    HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);                                //优先级  
	
	DMA_Init_Handle_uart_tx.Instance = DMA1_Stream6;                              //DMA类似于纵坐标
    DMA_Init_Handle_uart_tx.Init.Channel = DMA_CHANNEL_4;                         //DMA类似于横坐标详细看手册
    DMA_Init_Handle_uart_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; 			      //内存到外设
	DMA_Init_Handle_uart_tx.Init.PeriphInc = DMA_PINC_DISABLE;     			   	  //不递增地址
    DMA_Init_Handle_uart_tx.Init.MemInc = DMA_MINC_DISABLE;         		      //递增地址
    DMA_Init_Handle_uart_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;   //半字节
    DMA_Init_Handle_uart_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;      //半字节
    DMA_Init_Handle_uart_tx.Init.Mode = DMA_NORMAL;                         //DMA_CIRCULAR ,DMA_NORMAL          循环转换
    DMA_Init_Handle_uart_tx.Init.Priority = DMA_PRIORITY_HIGH;                    //速度
    DMA_Init_Handle_uart_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;                                                      //初始化dma
	//这里直接强硬调用底层接口，如果使用封装函数直接去读DR寄存器会清楚EOC标志位
	 __HAL_LINKDMA(&huart2,hdmatx,DMA_Init_Handle_uart_tx);	                  //链接adc总控结构体
	 HAL_DMA_Init(&DMA_Init_Handle_uart_tx);  
    HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 5, 0);                        //优先级
    HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);     
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
 
    __HAL_RCC_USART1_CLK_DISABLE();

   
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

  
  }
}
void uartprintf(const char *fmt,...){
	uint8_t tempbuff[256];
	uint16_t i;
	va_list ap;
	va_start(ap,fmt);
	vsprintf((char *)tempbuff,fmt,ap);
	va_end(ap);
	
	for(i=0;i<strlen((char *)tempbuff);i++){
		while(!__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE));
		huart1.Instance->DR = tempbuff[i];
	}
	while(!__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC));
}
uint32_t *temp;


void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{

}
